package me.yushuai.wukong.base.controller;

import me.yushuai.wukong.base.api.LocGroupApi;
import me.yushuai.wukong.base.api.dto.CreateLocGroupDTO;
import me.yushuai.wukong.base.api.dto.LocGroupDTO;
import me.yushuai.wukong.base.api.dto.UpdateLocGroupDTO;
import me.yushuai.wukong.base.api.request.PagingSearchLocGroupRequest;
import me.yushuai.wukong.base.api.vo.LocGroupVO;
import me.yushuai.wukong.base.entity.LocGroup;
import me.yushuai.wukong.base.service.LocGroupService;
import me.yushuai.wukong.common.dto.PageDTO;
import me.yushuai.wukong.common.error.WuKongException;
import me.yushuai.wukong.common.mvc.BaseController;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 库位组控制器
 *
 * @author zhoushuai@189.cn
 * @since 2022-08-31
 */
@RestController
public class LocGroupController extends BaseController implements LocGroupApi {

    /**
     * 库位组业务类
     */
    private final LocGroupService locGroupService;

    @Autowired
    public LocGroupController(LocGroupService locGroupService) {
        this.locGroupService = locGroupService;
    }

    @Override
    public Long createLocGroup(CreateLocGroupDTO createLocGroupDTO) throws WuKongException {
        return this.locGroupService.createLocGroup(createLocGroupDTO);
    }

    @Override
    public Long editLocGroup(Long locGroupId, UpdateLocGroupDTO updateLocGroupDTO)
            throws WuKongException {
        updateLocGroupDTO.setGroupId(locGroupId);
        return this.locGroupService.updateLocGroup(updateLocGroupDTO);
    }

    @Override
    public Long deleteLocGroup(Long locGroupId, Long version) throws WuKongException {
        this.locGroupService.deleteLocGroup(locGroupId, version);
        return locGroupId;
    }

    @Override
    public LocGroupVO getById(Long locGroupId) throws WuKongException {
        final LocGroupDTO locGroup = this.locGroupService.findOne(locGroupId);
        final LocGroupVO result = new LocGroupVO();
        BeanUtils.copyProperties(locGroup, result);
        return result;
    }

    @Override
    public PageDTO<LocGroupVO> pagingSearchLocGroup(PagingSearchLocGroupRequest pagingSearchLocGroup) {

        Specification<LocGroup> spec = (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList();

            final Long[] groupIds = pagingSearchLocGroup.getGroupIds();
            if (Objects.nonNull(groupIds) && groupIds.length > 0) {
                predicates.add(criteriaBuilder.in(root.get("groupId")).in(groupIds));
            }

            return criteriaBuilder.and(predicates.toArray(Predicate[]::new));
        };

        PageDTO<LocGroupDTO> locGroupPage = this.locGroupService.pagingSearchLocGroup(pagingSearchLocGroup, spec);
        List<LocGroupVO> locGroupList = locGroupPage.getData().stream().map(v -> {
            final LocGroupVO vo = new LocGroupVO();
            BeanUtils.copyProperties(v, vo);
            return vo;
        }).collect(Collectors.toList());
        return PageDTO.of(pagingSearchLocGroup, locGroupList);
    }
}
